package com.example.comm.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.comm.Bean.Post;
import com.example.comm.R;
import com.example.comm.activity.Login;
import com.example.comm.activity.Receive;

import java.util.List;

import cn.bmob.v3.BmobUser;

public class MyPushAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<Post> data;

    private final int N_TYPE=0;
    private final int F_TYPE=1;

    private int Max_num=15;
    private Boolean isfootview=true;//是否有footview

    public MyPushAdapter(Context context,List<Post> data){
        this.context=context;
        this.data=data;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.mypush_item,parent,false);
        View footview=LayoutInflater.from(view.getContext()).inflate(R.layout.foot_item,parent,false);
        if(viewType==F_TYPE){
            return new MyPushAdapter.RecyclerViewHolder(footview,F_TYPE);
        }else{
            return new MyPushAdapter.RecyclerViewHolder(view,N_TYPE);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(isfootview&& (getItemViewType(position))==F_TYPE){
            final MyPushAdapter.RecyclerViewHolder recyclerViewHolder=(MyPushAdapter.RecyclerViewHolder)holder;
            recyclerViewHolder.Loading.setText("加载中...");
            Handler handler=new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Max_num+=8;
                    notifyDataSetChanged();
                }
            },2000);
        }else{
            //ord_item的内容
            final MyPushAdapter.RecyclerViewHolder recyclerViewHolder=(MyPushAdapter.RecyclerViewHolder)holder;
            final Post post=data.get(position);
            recyclerViewHolder.username.setText(post.getName());
            recyclerViewHolder.nickname.setText(post.getNickname());
            recyclerViewHolder.content.setText(post.getContent());
            recyclerViewHolder.time.setText(post.getCreatedAt());

            recyclerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position=recyclerViewHolder.getAdapterPosition();
                    if(BmobUser.getCurrentUser(BmobUser.class)!=null){//用户存在


                        Intent in=new Intent(context, Receive.class);
                        in.putExtra("username",post.getName());
                        in.putExtra("content",post.getContent());
                        in.putExtra("time",post.getCreatedAt());
                        in.putExtra("id",data.get(position).getObjectId());
                        context.startActivity(in);
                    }else {
                        Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
                        context.startActivity(new Intent(context, Login.class));
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==Max_num-1){//到了最底下
            return F_TYPE;
        }
        else return N_TYPE;
    }

    @Override
    public int getItemCount() {
        if(data.size()<Max_num){
            return  data.size();
        }
        return Max_num;
    }
    private class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView nickname,content,time,username;//ord_item的TextView
        public TextView Loading;

        public RecyclerViewHolder(View itemview, int view_type) {
            super(itemview);
            if(view_type==N_TYPE){
                username=itemview.findViewById(R.id.mypush_username);
                nickname=itemview.findViewById(R.id.mypush_nickname);
                content=itemview.findViewById(R.id.mypush_content);
                time=itemview.findViewById(R.id.mypush_time);
            }else if(view_type==F_TYPE){
                Loading=itemview.findViewById(R.id.footText);
            }
        }
    }
}
